package net.hn.hnms.biz.performance.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import net.hn.hnms.biz.performance.domain.PerformanceScoringDetails;
import net.hn.hnms.biz.performance.mapper.PerformanceScoringDetailsMapper;
import net.hn.hnms.biz.performance.request.PerformanceScoringDetailsReq;
import net.hn.hnms.biz.performance.service.PerformanceScoringDetailsService;
import net.hn.hnms.biz.performance.vo.PerformanceScoringDetailsVo;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PerformanceScoringDetailsServiceImpl implements PerformanceScoringDetailsService {

    @Autowired
    private PerformanceScoringDetailsMapper performanceScoringDetailsMapper;

    @Override
    public Page<PerformanceScoringDetailsVo> getPerformanceScoringDetails(PageQuery pageQuery, PerformanceScoringDetailsReq req) {
//        QueryWrapper
        LambdaQueryWrapper<PerformanceScoringDetails> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(PerformanceScoringDetailsVo::getPerformanceScoringId, req.getPerformanceScoringId());
        lqw.eq(StrUtil.isNotBlank(req.getScoringDate()),PerformanceScoringDetails::getScoringDate, req.getScoringDate());
        lqw.eq(StrUtil.isNotBlank(req.getScoringType()),PerformanceScoringDetails::getScoringType, req.getScoringType());
        lqw.eq(StrUtil.isNotBlank(req.getPerformanceTemplateId()),PerformanceScoringDetails::getPerformanceTemplateId, req.getPerformanceTemplateId());
        lqw.eq(StrUtil.isNotBlank(req.getPerformanceScoringId()),PerformanceScoringDetails::getPerformanceScoringId, req.getPerformanceScoringId());
        lqw.orderBy(true, true, PerformanceScoringDetails::getPerformanceScoringDetailsId);
        Page<PerformanceScoringDetails> page1=performanceScoringDetailsMapper.selectPage(pageQuery.build(),lqw);
        Page<PerformanceScoringDetailsVo> page2 = new Page<>();

        List<PerformanceScoringDetailsVo> voList = page1.getRecords().stream()
                .map(details -> {
                    PerformanceScoringDetailsVo vo = new PerformanceScoringDetailsVo();
//                    details.setScoreItems(details.getScoreItems().replace("\\", ""));
                    BeanUtils.copyProperties(details, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        page2.setRecords(voList);
        page2.setTotal(page1.getTotal());
        return page2;
    }


    @Override
    public int delete(List<String> ids) {
        return performanceScoringDetailsMapper.deleteBatchIds(ids);
    }

    @Override
    public int update(PerformanceScoringDetailsReq req) {

        PerformanceScoringDetails p = performanceScoringDetailsMapper.selectById(req.getPerformanceScoringDetailsId());
        if (p == null) {return 0;}
        p.setName(req.getScoreItems().get("name"));
        p.setScoreItems(JSON.toJSONString(req.getScoreItems()));
        return performanceScoringDetailsMapper.updateById(p);
    }

    public static void main(String[] args) throws Exception {
        // 原始的 JSON 字符串
        String jsonString = "{\"name\":\"李四333\",\"attendanceDays\":\"23\",\"clockingDays\":\"22\",\"daysOfDelay\":\"2\"}";

        // 使用 Jackson 的 ObjectMapper 解析 JSON 字符串为 JsonNode 对象
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(jsonString);

        // 修改 JsonNode 对象中的指定字段的值
        ((ObjectNode) jsonNode).put("attendanceDays", "25");
        ((ObjectNode) jsonNode).put("clockingDays", "24");

        // 将修改后的 JsonNode 对象转换回 JSON 字符串
        String modifiedJsonString = objectMapper.writeValueAsString(jsonNode);

        // 打印修改后的 JSON 字符串
        System.out.println(modifiedJsonString);
    }
}
